using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Infrastructure.Interface;
using Infrastructure;

namespace Domain.Repository
{
    /// <summary>
    /// 分页信息  
    /// <remarks></remarks>
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    public class PageInfo<T> : PageBase<T>
    {
        /// <summary>
        /// 分页信息
        /// </summary>
        /// <param name="totalCount">所有条目</param>
        /// <param name="entityArray">分页数据</param>
        public PageInfo(int totalCount, List<T> entityArray)
        {
            this.TotalCount = totalCount;
            this.EntityArray = entityArray;
        }

        /// <summary>
        /// 转换为兼容Easy-UI表格的Json字符串
        /// </summary>
        /// <returns></returns>
        public override string ToEasyUiJson()
        {
            var format = "yyyy-MM-dd";
            return this.ToEasyUiJson(format);
        }

        /// <summary>
        /// 转换为兼容Easy-UI表格的Json字符串
        /// </summary>
        /// <param name="dataFormat">时间格式</param>
        /// <returns></returns>
        public override string ToEasyUiJson(string dataFormat)
        {
            // 兼容Easy-UI数据格式
            var data = new { total = this.TotalCount, rows = this.EntityArray };
            string json = JsonSerializer.ToJson(data, dataFormat);
            return json;
        }

        /// <summary>
        /// 转换为兼容MINI-UI表格的Json字符串
        /// </summary>     
        /// <returns></returns>
        public override string ToMiniUiJson()
        {
            var format = "yyyy-MM-dd";
            return this.ToMiniUiJson(format);
        }

        /// <summary>
        /// 转换为兼容MINI-UI表格的Json字符串
        /// </summary>
        /// <param name="dataFormat">时间格式</param>
        /// <returns></returns>
        public override string ToMiniUiJson(string dataFormat)
        {
            // 兼容Easy-UI数据格式
            var data = new { total = this.TotalCount, data = this.EntityArray };
            string json = JsonSerializer.ToJson(data, dataFormat);
            return json;
        }

        /// <summary>
        /// 总记录条目
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("TotalCount={0}", this.TotalCount);
        }
    }
}
